package com.asteros.crypto.utils.content;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSysObject;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.DfLoginInfo;
import com.documentum.fc.common.IDfLoginInfo;

public class WebServerCMUtils_Prod implements WebServerCMUtils
{
	protected static final String docbase = "ARCH";
	protected static final String user = "dmadmin";
	protected static final String password = "rkk";
	private IDfSession session;

	private IDfSession getSession() {
		try {
			IDfClientX cx = new DfClientX();
			IDfClient client;
			client = cx.getLocalClient();
			IDfLoginInfo login = new DfLoginInfo();
			login.setUser(user);
			login.setPassword(password);

			session = client.newSession(docbase, login);
		} catch (DfException e) {
			e.printStackTrace();
		}
		return session;
	}

	public byte[] getContent(String userId, String documentId) throws Exception  {
		System.out.println("WebServerCMUtils_Stub.getContent>>");

		byte[] buf = null;
		try {
			IDfSysObject object = getContentObjectByDocumentId(getSession(), documentId);
			buf = this.getDataFromObject(object);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			
			if(session !=null) session.disconnect();
		}

		System.out.println("WebServerCMUtils_Stub.getContent<<");
		return buf;
	}

	public byte[] getSignedData(String userId, String documentId) throws Exception {
		System.out.println("WebServerCMUtils_Stub.getSignedData>>");

		byte[] buf = null;
		try {
			IDfSysObject sign = getSignObjectByDocumentId(getSession(), documentId);
			buf = this.getDataFromObject(sign);
		}finally{
			if(session !=null) session.disconnect();
		}

		System.out.println("WebServerCMUtils_Stub.getSignedData<<");
		return buf;
	}

	public void saveSignedData(String userId, String documentId, byte[] signedData) throws Exception {
		System.out.println("WebServerCMUtils_Stub.saveSignedData>>");

		try {
			IDfSysObject sign = getSignObjectByDocumentId(getSession(), documentId);
			if (null == sign) {
				sign = (IDfSysObject) session.newObject("ddt_sign");
				sign.setObjectName(documentId + "_signature");
				sign.setString("dsid_document", documentId);
				sign.link("/Office Utils/Signatures");
				sign.setContentType("crtext"); 
			}
			saveDataToObject(sign, signedData);
		}finally{
			if(session !=null) session.disconnect();
		}
		System.out.println("WebServerCMUtils_Stub.saveSignedData<<");
	}


	private void saveDataToObject(IDfSysObject object, byte[] data) throws Exception{
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		output.write(data);
		object.setContent(output);

		object.save();
	}

	private byte[] getDataFromObject(IDfSysObject object) throws Exception{
		byte[] bytes = null;
		if (object != null) {
			ByteArrayInputStream input = object.getContent();

			bytes = new byte[input.available()];
			input.read(bytes);
			System.out.print(new String(bytes));
		}
		return bytes;
	}

	private IDfSysObject getContentObjectByDocumentId(IDfSession session, String documentId) throws DfException {
		return (IDfSysObject) session.getObject(new DfId(documentId));
	}

	private IDfSysObject getSignObjectByDocumentId(IDfSession session, String documentId) throws DfException {
		String dql = "ddt_sign WHERE dsid_document = '" + documentId + "'";
		return (IDfSysObject) session.getObjectByQualification(dql);
	}

}

